#include <bits/stdc++.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <unistd.h>
#include <fcntl.h>
#include "scb_can.h"

zucab_internal_info_t* shmmap(const char* filename)
{
    int fd = shm_open(filename, O_CREAT | O_RDWR, 0666);
    if (fd == -1)
    {
        std::cerr << "shm_open failed: " << strerror(errno) << std::endl;
        return nullptr;
    }
    if (ftruncate(fd, sizeof(zucab_internal_info_t)))
    {
        std::cerr << "ftruncate failed: " << strerror(errno) << std::endl;
        close(fd);
        return nullptr;
    }
    zucab_internal_info_t* ret = (zucab_internal_info_t*)mmap(0, sizeof(zucab_internal_info_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    close(fd);
    if (ret == MAP_FAILED)
    {
        std::cerr << "mmap failed: " << strerror(errno) << std::endl;
        return nullptr;
    }
    return ret;
}